package br.com.toscano.contpedido.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import br.com.toscano.contpedido.business.StatusPedidoBC;
import br.com.toscano.contpedido.constant.ConstantesMensagem;
import br.com.toscano.contpedido.domain.StatusPedido;
import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.util.ResourceBundle;

@ViewController
@NextView("./statusPedido_edit.jsf")
@PreviousView("./statusPedido_list.jsf")
public class StatusPedidoListMB extends AbstractListPageBean<StatusPedido, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private StatusPedidoBC statusPedidoBC;
	
	@Override
	protected List<StatusPedido> handleResultList() {
		return this.statusPedidoBC.findAll();
	}
	
	@Inject
	private FacesContext facesContext;
	
	@Inject
	private ResourceBundle bundle;

	public String deleteSelection() {
		boolean delete;
		
		List<Long> ids = new ArrayList<Long>();
		for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
			Long id = iter.next();
			delete = getSelection().get(id);
			if (delete) {
				ids.add(id);
				iter.remove();
			}
		}
		
		try {
			statusPedidoBC.delete(ids);
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage(bundle.getString(ConstantesMensagem.ERRO_REMOVER)));
		}				

		return getPreviousView();
	}

}